package com.xauto.pioneer.appuser.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

/**
 * 申请停车发票适配器
 * Desc:订单列表 适配器
 */
public class ApplyParkingReceiptAdapter extends BaseAdapter {

    private Context context;
    private List<HashMap<String,Object>> list;
    private LayoutInflater layoutInflater;
    private TextView tv_applyparkingreceipt_location;
    private TextView tv_applyparkingreceipt_price;
    private TextView tv_applyparkingreceipt_carnum;
    private TextView tv_applyparkingreceipt_time;
    private TextView tv_applyparkingreceipt_start;
    private TextView tv_applyparkingreceipt_end;
    private CheckBox cb_applyparkingreceipt;
    public ApplyParkingReceiptAdapter(Activity context,List<HashMap<String, Object>> list) {
        this.context = context;
        this.list = list;//list中checkbox状态为false
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = layoutInflater.inflate(R.layout.item_applyparkingreceipt, null);

            ViewCache viewCache = new ViewCache();
            tv_applyparkingreceipt_location = (TextView) convertView.findViewById(R.id.tv_applyparkingreceipt_location);
            tv_applyparkingreceipt_price = (TextView) convertView.findViewById(R.id.tv_applyparkingreceipt_price);
            tv_applyparkingreceipt_carnum = (TextView) convertView.findViewById(R.id.tv_applyparkingreceipt_carnum);
            tv_applyparkingreceipt_time = (TextView) convertView.findViewById(R.id.tv_applyparkingreceipt_time);
            tv_applyparkingreceipt_start = (TextView) convertView.findViewById(R.id.tv_applyparkingreceipt_start);
            tv_applyparkingreceipt_end = (TextView) convertView.findViewById(R.id.tv_applyparkingreceipt_end);
            cb_applyparkingreceipt = (CheckBox) convertView.findViewById(R.id.cb_applyparkingreceipt);

            viewCache.tv_applyparkingreceipt_location = tv_applyparkingreceipt_location;
            viewCache.tv_applyparkingreceipt_price = tv_applyparkingreceipt_price;
            viewCache.tv_applyparkingreceipt_carnum = tv_applyparkingreceipt_carnum;
            viewCache.tv_applyparkingreceipt_time = tv_applyparkingreceipt_time;
            viewCache.tv_applyparkingreceipt_start = tv_applyparkingreceipt_start;
            viewCache.tv_applyparkingreceipt_end = tv_applyparkingreceipt_end;
            viewCache.cb_applyparkingreceipt = cb_applyparkingreceipt;
            convertView.setTag(viewCache);
        }else
        {
            ViewCache viewCache = (ViewCache) convertView.getTag();
            tv_applyparkingreceipt_location = viewCache.tv_applyparkingreceipt_location;
            tv_applyparkingreceipt_price = viewCache.tv_applyparkingreceipt_price;
            tv_applyparkingreceipt_carnum = viewCache.tv_applyparkingreceipt_carnum;
            tv_applyparkingreceipt_time = viewCache.tv_applyparkingreceipt_time;
            tv_applyparkingreceipt_start =viewCache. tv_applyparkingreceipt_start;
            tv_applyparkingreceipt_end = viewCache.tv_applyparkingreceipt_end;
            cb_applyparkingreceipt = viewCache.cb_applyparkingreceipt;
        }
        try {
            JSONObject js = new JSONObject(list.get(position).get("name").toString());
            tv_applyparkingreceipt_location.setText(js.getString("receipt_applyparkingorder_location"));
            tv_applyparkingreceipt_price.setText(js.getString("receipt_applyparkingorder_price"));
            tv_applyparkingreceipt_carnum.setText(js.getString("receipt_applyparkingorder_carnum") );
            tv_applyparkingreceipt_time.setText(js.getString("receipt_applyparkingorder_time"));
            tv_applyparkingreceipt_start.setText(js.getString("receipt_applyparkingorder_start"));
            tv_applyparkingreceipt_end.setText(js.getString("receipt_applyparkingorder_end"));
            cb_applyparkingreceipt.setChecked((Boolean) list.get(position).get("boolean"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    public class ViewCache{
        TextView tv_applyparkingreceipt_location;
        TextView tv_applyparkingreceipt_price;
        TextView tv_applyparkingreceipt_carnum;
        TextView tv_applyparkingreceipt_time;
        TextView tv_applyparkingreceipt_start;
         TextView tv_applyparkingreceipt_end;
         public CheckBox cb_applyparkingreceipt;
    }
}
